import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/a_yyt/models/policy_deal_detail_entity.dart';
import 'package:ls_work/a_yyt/models/policy_warning_entity.dart';
import 'package:ls_work/mvp/svp_progress.dart';
import 'package:ls_work/net/HttpUtil.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/res/gaps.dart';
import 'package:ls_work/res/styles.dart';
import 'package:ls_work/util/toast.dart';

import '../../net/api/http_api.dart';

class PolicyRecordDetailPage extends StatefulWidget {
  final PolicyWarningInfo? info;
  PolicyRecordDetailPage({Key? key, this.info});
  @override
  _PolicyRecordDetailPageState createState() => _PolicyRecordDetailPageState();
}

class _PolicyRecordDetailPageState extends State<PolicyRecordDetailPage> {
  List<PolicattAchListInfo> attachList = [];
  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _fectData();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
            '${widget.info?.customerName??""} ${widget.info?.floorNo??""} ${widget.info?.roomNo??""}'),
        elevation: 0.5,
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.all(15),
        child: Container(
          width: double.infinity,
          margin: EdgeInsets.only(top: 15),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(8),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Text(
                    '${widget.info?.customerName ?? ''}',
                    style: TextStyles.textDark18,
                  ),
                  Gaps.hGap10,
                  Expanded(
                    child: Text(
                      '${widget.info?.floorNo??""}${widget.info?.roomNo??""}',
                      style: TextStyles.textGray14,
                    ),
                  ),
                ],
              ),
              Gaps.vGap12,
              Gaps.vGap12,
              Text(
                '首次呼叫： ${DateUtil.formatDateStr(widget.info?.createdDate??"", format: 'MM-dd HH:mm:ss')}',
                style: TextStyles.textGray18,
              ),
              Gaps.vGap12,
              Text.rich(TextSpan(children: [
                TextSpan(
                  text: '本次报警 ',
                  style: TextStyles.textGray18,
                ),
                TextSpan(
                  text: '${widget.info?.callTimes??""}',
                  style: TextStyle(fontSize: 18, color: Colors.red),
                ),
                TextSpan(
                  text: ' 次呼叫',
                  style: TextStyles.textGray18,
                ),
              ])),
              Gaps.vGap12,
              Text(
                '最后呼叫： ${DateUtil.formatDateStr(widget.info?.lastCallTime??"", format: 'MM-dd HH:mm:ss')}',
                style: TextStyles.textGray18,
              ),
              Gaps.vGap12,
              Text(
                '首次应答： ${DateUtil.formatDateStr(widget.info?.answerTime??"", format: 'MM-dd HH:mm:ss')}',
                style: TextStyles.textGray18,
              ),
              Gaps.vGap12,
              Text(
                '处理时间： ${DateUtil.formatDateStr(widget.info?.dealTime??"", format: 'MM-dd HH:mm:ss')}',
                style: TextStyles.textGray18,
              ),
              Gaps.vGap12,
              Text(
                '处理时长： ${widget.info?.handlingTime??""}',
                style: TextStyles.textGray18,
              ),
              Gaps.vGap10,
              Gaps.vGap10,
            ],
          ),
        ),
      ),
    );
  }

  _fectData() {
    var map = {'id': widget.info?.id};

    SVProgressHUD page = SVProgressHUD(context);
    page.showProgress();
    HttpUtil.instance
        .getDio()
        .post(HttpApi.policyWarningDetail, data: map,options: PostRequest.urlencodedHeader)
        .then((r) {
      ///
      page.closeProgress();
      if (r.statusCode == 200) {
        Map<String, dynamic> map = json.decode(r.toString());
        if (map["code"].toString() == "0") {
          if (map['data'] != null) {
            PolicyDealEntity model = PolicyDealEntity.fromJson(map['data']);
            setState(() {
              attachList = model.attachList??[];
            });
          }
        } else {
          Toast.show(map["msg"]);
        }
      } else {
        Toast.show(r.statusCode.toString());
      }
    }).catchError((e) {
      page.closeProgress();
      print(e.toString());
      Toast.show(getErrorMessage(e));
    });
  }
}
